package com.example.smartpark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.smartpark.common.Result;
import com.example.smartpark.dto.UserAuditDTO;
import com.example.smartpark.dto.UserAuditQueryDTO;
import com.example.smartpark.service.UserAuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户审核Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/user-audit")
public class UserAuditController {

    @Autowired
    private UserAuditService userAuditService;

    /**
     * 获取用户审核列表
     *
     * @param queryDTO 查询参数
     * @return 分页列表
     */
    @GetMapping
    public Result<IPage<UserAuditDTO>> getUserAuditList(UserAuditQueryDTO queryDTO) {
        log.info("获取用户审核列表: {}", queryDTO);
        IPage<UserAuditDTO> page = userAuditService.getUserAuditPage(queryDTO);
        return Result.success(page);
    }

    /**
     * 获取用户审核详情
     *
     * @param id 审核ID
     * @return 审核详情
     */
    @GetMapping("/{id}")
    public Result<UserAuditDTO> getUserAuditDetail(@PathVariable Integer id) {
        log.info("获取用户审核详情: id={}", id);
        UserAuditDTO userAuditDTO = userAuditService.getUserAuditById(id);
        return Result.success(userAuditDTO);
    }

    /**
     * 处理用户审核
     *
     * @param id     审核ID
     * @param status 状态
     * @param remark 备注
     * @return 处理结果
     */
    @PutMapping("/{id}")
    public Result<UserAuditDTO> processUserAudit(
            @PathVariable Integer id,
            @RequestParam Integer status,
            @RequestParam(required = false) String remark,
            @RequestParam(required = false) Integer auditorId) {
        log.info("处理用户审核: id={}, status={}, remark={}, auditorId={}", id, status, remark, auditorId);
        
        // 如果没有指定审核人，默认为系统管理员(ID=1)
        if (auditorId == null) {
            auditorId = 1;
        }
        
        UserAuditDTO userAuditDTO = userAuditService.processUserAudit(id, status, remark, auditorId);
        return Result.success(userAuditDTO);
    }

    /**
     * 创建用户审核记录
     *
     * @param userAuditDTO 审核信息
     * @return 创建结果
     */
    @PostMapping
    public Result<UserAuditDTO> createUserAudit(@RequestBody @Valid UserAuditDTO userAuditDTO) {
        log.info("创建用户审核记录: {}", userAuditDTO);
        UserAuditDTO created = userAuditService.createUserAudit(userAuditDTO);
        return Result.success(created);
    }
} 